//
//  UserDefaultWrapper.swift
//  PokeMaster
//
//  Created by 李腾芳 on 2022/1/12.
//  Copyright © 2022 OneV's Den. All rights reserved.
//

import Foundation
import Combine

@propertyWrapper
struct UserDefaultsStorage<T: Any> {
    let key: String
    var value: T
    
    init(key: String, defaultValue: T) {
        self.key = key
        value = UserDefaults.standard.value(forKey: key) as? T ?? defaultValue
    }
    
    var wrappedValue: T {
        get {
            value
        }
        
        set {
            value = newValue
            UserDefaults.standard.set(value, forKey: key)
        }
    }
}

